using System.Drawing;
using System.IO;
using NUnit.Framework;
using OpenWaves.ImageTransformations;
using ImageFormat = OpenWaves.ImageTransformations.ImageFormat;

namespace OpenWaves.UnitTests.ImageTransformations
{
    [TestFixture]
    public class GdiImageTransformationServiceTests
    {
        [Test]
        public void TestGifImageTransformation()
        {
            var service = new GdiImageTransformationService();
            var output = new MemoryStream();
            service.TransformImage(new MemoryStream(Resources.SampleGifImage), output, ImageFormat.Png, new ScaleToFillTransformation(100, 100));
            output.Seek(0, SeekOrigin.Begin);
            Image.FromStream(output);
        }

        [Test]
        public void TestPngImageTransformation()
        {
            var service = new GdiImageTransformationService();
            var output = new MemoryStream();
            service.TransformImage(new MemoryStream(Resources.SamplePngImage), output, ImageFormat.Png, new ScaleToFillTransformation(100, 100));
            output.Seek(0, SeekOrigin.Begin);
            Image.FromStream(output);
        }

        [Test]
        public void TestScalingUpImageTransformation()
        {
            const int resultSize = 270;

            var service = new GdiImageTransformationService();

            var inputStream = new MemoryStream(Resources.WhiteSquare);
            var input = (Bitmap)Image.FromStream(inputStream);
            inputStream.Seek(0, SeekOrigin.Begin);
            var outputStream = new MemoryStream();
            service.TransformImage(inputStream, outputStream, ImageFormat.Png, new ScaleToFillTransformation(resultSize, resultSize));
            outputStream.Seek(0, SeekOrigin.Begin);
            var result = (Bitmap)Image.FromStream(outputStream);

            var sourceColor = input.GetPixel(0, 0);

            Assert.AreEqual(resultSize, result.Width);
            Assert.AreEqual(resultSize, result.Width);

            Assert.AreEqual(sourceColor, result.GetPixel(0, 0));
            Assert.AreEqual(sourceColor, result.GetPixel(resultSize - 1, resultSize - 1));

            //check on edges
            Assert.AreEqual(sourceColor, result.GetPixel(resultSize - 1, (resultSize - 1) / 2));
            Assert.AreEqual(sourceColor, result.GetPixel((resultSize - 1) / 2, resultSize - 1));
            Assert.AreEqual(sourceColor, result.GetPixel(0, (resultSize - 1) / 2));
            Assert.AreEqual(sourceColor, result.GetPixel((resultSize - 1) / 2, 0));

            Assert.IsNotNull(result);
        }

        [Test]
        public void TestOutOfMemoryImage()
        {
            var service = new GdiImageTransformationService();
            var output = new MemoryStream();
            service.TransformImage(new MemoryStream(Resources.ImageWithUnrecognizedPixelFormat), output, ImageFormat.Png, new ScaleToFitTransformation(179, 179));
            output.Seek(0, SeekOrigin.Begin);
            Image.FromStream(output);
        }

    }
}